package com.forsir.android.mhfinance.helper;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.forsir.android.mhfinance.R;
import com.forsir.android.mhfinance.storeObjects.Account;
import com.forsir.android.mhfinance.storeObjects.AccountsList;
import com.forsir.android.mhfinance.storeObjects.Category;
import com.forsir.android.mhfinance.storeObjects.IStorable;
import com.forsir.android.mhfinance.storeObjects.Payment;
import com.forsir.android.mhfinance.storeObjects.Repeating;
import com.forsir.android.mhfinance.storeObjects.Subcategory;

/**
 * class, which provide operations with category
 */
public abstract class ManageItemsActivity extends Activity {
	protected DbAdapter mDbHelper;

	private IStorable mItem;

	AlertDialog mConfirmDialog;
	AlertDialog mTextDialog;

	protected SharedPreferences mPreferenceSettings;
	protected String mAddNewType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDbHelper = new DbAdapter(this, this);
		mDbHelper.open();

		// Restore preferences
		mPreferenceSettings = PreferenceManager.getDefaultSharedPreferences(this);
		mAddNewType = mPreferenceSettings.getString(getString(R.string.key_new_item), getResources().getStringArray(R.array.preferences_new)[0]);
	}

	public void ShowAddNewDialog(IStorable item) {
		int title = 0;
		if (item.getClass() == Account.class) {
			title = R.string.menu_account_new;
		}
		if (item.getClass() == Category.class) {
			title = R.string.menu_category_new;
		}
		if (item.getClass() == Subcategory.class) {
			title = R.string.menu_subcategory_new;
		}
		if (item.getClass() == Payment.class) {
			title = R.string.menu_payment_new;
		}

		final LayoutInflater factory = LayoutInflater.from(this);
		final View textEntryView = factory.inflate(R.layout.dialog, null);

		final EditText text = (EditText) textEntryView.findViewById(R.id.dialog_edit_text);
		text.setHint(R.string.dialog_enter_new_name);
		final TextView viewText = (TextView) textEntryView.findViewById(R.id.dialog_text_view);
		viewText.setText(R.string.dialog_enter_name);

		final Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setView(textEntryView);
		builder.setPositiveButton(R.string.save, addNewItemClickListener);
		builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		mTextDialog = builder.show();
	}

	public void ShowEditDialog(IStorable item) {
		int title = 0;
		if (item.getClass() == Account.class) {
			title = R.string.menu_account_edit;
		}
		if (item.getClass() == Category.class) {
			title = R.string.menu_category_edit;
		}
		if (item.getClass() == Subcategory.class) {
			title = R.string.menu_subcategory_edit;
		}
		if (item.getClass() == Payment.class) {
			title = R.string.menu_payment_edit;
		}

		final LayoutInflater factory = LayoutInflater.from(this);
		final View textEntryView = factory.inflate(R.layout.dialog, null);

		final EditText text = (EditText) textEntryView.findViewById(R.id.dialog_edit_text);
		text.setText(item.getName());
		final TextView viewText = (TextView) textEntryView.findViewById(R.id.dialog_text_view);
		viewText.setText(R.string.dialog_enter_new_name);

		final Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setView(textEntryView);
		builder.setPositiveButton(R.string.save, editItemClickListener);
		builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
			}
		});
		mItem = item;
		mTextDialog = builder.show();
	}

	public void ShowDeleteDialog(IStorable item) {
		int title = 0;
		CharSequence text = "";
		if (item.getClass() == Account.class) {
			title = R.string.menu_account_delete;
			text = getText(R.string.menu_account_delete_confirm);
		}
		if (item.getClass() == Category.class) {
			title = R.string.menu_category_delete;
			text = getText(R.string.menu_category_delete_confirm);
		}
		if (item.getClass() == Subcategory.class) {
			title = R.string.menu_subcategory_delete;
			text = getText(R.string.menu_subcategory_delete_confirm);
		}
		if (item.getClass() == Payment.class) {
			title = R.string.menu_payment_delete;
			text = getText(R.string.menu_payment_delete_confirm);
		}
		if (item.getClass() == Repeating.class) {
			title = R.string.menu_repeating_delete;
			text = getText(R.string.menu_repeating_delete_confirm);
		}

		text = String.format(text.toString(), item.getName());
		mItem = item;

		final Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setMessage(text);
		builder.setPositiveButton(R.string.yes, deleteItemClickListener);
		builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { // nothing happened
			        public void onClick(DialogInterface dialog, int whichButton) {
			        }
		        });
		builder.show();
	}

	// handle context menu
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

		switch (item.getItemId()) {
			case R.id.menu_account_new:
				ShowAddNewDialog(new Account());
				break;
			case R.id.menu_account_delete:
				ShowDeleteDialog(AccountsList.getAccount(info.position));
				break;
			case R.id.menu_account_edit:
				ShowEditDialog(AccountsList.getAccount(info.position));
				break;

		}
		return true;
	}

	public void deleteItem(IStorable item) {
		ShowDeleteDialog(item);
	}

	public void addNewItem(IStorable item) {
		ShowAddNewDialog(item);
	}

	public void editItem(IStorable item) {
		ShowEditDialog(item);
	}

	private final OnClickListener addNewItemClickListener = new OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			final EditText text = (EditText) mTextDialog.findViewById(R.id.dialog_edit_text);
			final String returnText = text.getText().toString();

			if (returnText.length() != 0) {
				processAddNewItem(returnText);
			}
		}
	};

	private final OnClickListener editItemClickListener = new OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			final EditText text = (EditText) mTextDialog.findViewById(R.id.dialog_edit_text);
			final String returnText = text.getText().toString();

			if (returnText.length() != 0) {
				processEditItem(mItem, returnText);
			}
		}
	};

	private final OnClickListener deleteItemClickListener = new OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			processDeleteItem(mItem);
		}
	};

	// must be defined in child
	protected abstract void processAddNewItem(String name);

	protected abstract void processDeleteItem(IStorable item);

	protected abstract void processEditItem(IStorable item, String name);
}
